<?php

class Feedbacks extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
    function getAll(){
        $this->db->order_by("feedback_date", "asc");
        return $this->db->get('feedbacks');
    }
    
    function getAllNew(){
        $this->db->from('feedbacks as F');
        $this->db->join('users as U', 'U.user_id = F.user_id');
        $this->db->select('F.*, U.user_title, U.user_warnings');
        $this->db->order_by("F.feedback_date", "asc");
        $this->db->where("F.feedback_answered", 0);
        return $this->db->get();
    }
    
    function getFeedback($feedback_id){
        $this->db->from('feedbacks as F');
        $this->db->join('users as U', 'U.user_id = F.user_id');
        $this->db->select('F.*, U.user_title, U.user_warnings');
        $this->db->where("F.feedback_id", $feedback_id);
        return $this->db->get()->row();
    }
    
    function getFeedbackWithResponse($feedback_id){
        $this->db->from('feedbacks as F');
        $this->db->join('users as U', 'U.user_id = F.user_id');
        $this->db->join('staffusers as SU', 'SU.staffuser_id = F.staffuser_id');
        $this->db->select('F.*, SU.staffuser_title, U.user_title, U.user_warnings');
        $this->db->where("F.feedback_id", $feedback_id);
        $this->db->where("F.feedback_answered", 1);
        return $this->db->get()->row();
    }
    
    function respond($feedback_id,$feedback_response,$staffuser_id){
        $this->db->where("feedback_id",$feedback_id);
        $this->db->set("feedback_response",$feedback_response);
        $this->db->set("staffuser_id", $staffuser_id);
        $this->db->set("feedback_answered", 1);
        $this->db->update("feedbacks");
    }
    
    function getArchived($year,$month){
        //Get the days in the month so we can select them all
        $endOfMonth = days_in_month($month, $year);
        
        //Going from day 1 to end of month
        $dateStart = date_to_mysql($year, $month, 1);
        $dateEnd = date_to_mysql($year, $month, $endOfMonth);
        
        $this->db->from('feedbacks as F');
        $this->db->join('users as U', 'U.user_id = F.user_id');
        $this->db->join('staffusers as SU', 'SU.staffuser_id = F.staffuser_id');
        
        $this->db->where('F.feedback_date >=', $dateStart);
        $this->db->where('F.feedback_date <=', $dateEnd);
        $this->db->where('F.feedback_answered', "1");
        
        return $this->db->get();
    }
    
    function discardFeedback($feedback_id){
        $this->db->trans_start();
        
        $this->db->where("feedback_id",$feedback_id);
        $this->db->delete("feedbacks");
        
        $this->db->trans_complete();
        return $this->db->trans_status();
    }
    
    function insertFeedback($user_id, $feedback){
        $data['feedback_date'] = date_to_mysql(date('Y'), date('n'), date('j'));
        $data['feedback_comment'] = $feedback;
        $data['feedback_answered'] = 0; // Not answered
        $data['user_id'] = $user_id;
        
        $this->db->trans_start();
        $query = $this->db->insert('feedbacks', $data);
        $this->db->trans_complete();
        
        return $this->db->trans_status();
    }
}

